﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

namespace Web4Comment
{
    public partial class comments : System.Web.UI.Page
    {
        public int CommentType;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((String)Session["username"] == null || (String)Session["username"] == "")
            {
                Response.Redirect("./logon.aspx");
            }
            if (Session["CommentType"] != null)
            {
                CommentType = (int)Session["CommentType"];
            }
            else
            {
                CommentType = 1;
            }

            if (!IsPostBack)
            {
                List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments((String)Session["username"]);
                nowLabel.Text = "" + 1;
                allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
                listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
                CommentType = 1;
                Session["CommentType"] = 1;
                
            }
        }

        protected void HandleDeleteComment(object sender, EventArgs e)
        {
            Service.CommentService.deleteComment(System.Int32.Parse(((LinkButton)sender).CommandArgument));
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 1;
            Session["CommentType"] = 1;
        }

        protected void HandleDeleteRecommender(object sender, EventArgs e)
        {
            Service.CommentService.deleteRecommendation(System.Int32.Parse(((LinkButton)sender).CommandArgument));
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyRecommendation((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 4;
            Session["CommentType"] = 4;
        }

        protected void MyImageButton_Click(object sender, ImageClickEventArgs e)
        {
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 1;
            Session["CommentType"] = 1;
        }

        protected void FriendImageButton_Click(object sender, ImageClickEventArgs e)
        {
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getFriendComments((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 2;
            Session["CommentType"] = 2;
        }

        protected void RecommendImageButton_Click(object sender, ImageClickEventArgs e)
        {
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getFriendRecommendation((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 3;
            Session["CommentType"] = 3;
        }

        protected void MyRecommendImageButton_Click(object sender, ImageClickEventArgs e)
        {
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyRecommendation((String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
            listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
            CommentType = 4;
            Session["CommentType"] = 4;
        }

        private void listBind(List<UnlessComment.Model.CommentDetail> commentsList)
        {
            Collection<UnlessComment.Model.CommentDetail> comments = new Collection<UnlessComment.Model.CommentDetail>();

            if (commentsList == null || commentsList.Count == 0)
            {
                commentsdl.DataSource = null;
                commentsdl.DataBind();
                return;
            }

            for (int i = 0; i < commentsList.Count; i++)
            {
                comments.Add(commentsList[i]);
            }

            commentsdl.DataSource = comments;
            commentsdl.DataBind();
        }

        private List<UnlessComment.Model.CommentDetail> listByPage(int type)
        {
            switch (type)
            {
                case 1:
                    return Web4Comment.Service.CommentService.getMyComments((String)Session["username"]);
                case 2:
                    return Web4Comment.Service.CommentService.getFriendComments((String)Session["username"]);
                case 3:
                    return Web4Comment.Service.CommentService.getFriendRecommendation((String)Session["username"]);
                case 4:
                    return Web4Comment.Service.CommentService.getMyRecommendation((String)Session["username"]);
                default:
                    return null;
            }
        }

        protected void preButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now <= 1)
            {
                return;
            }
            now--;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = listByPage(CommentType);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));
            
        }

        protected void nextButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now >= all)
            {
                return;
            }
            now++;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = listByPage(CommentType);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));

        }

        protected void goButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            int go = System.Int32.Parse(goTextBox.Text);
            if (go > all || go < 1)
            {
                return;
            }
            now = go;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = listByPage(CommentType);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));
        }
    }
}